package com.luo.test.controller

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.web.bind.annotation.{GetMapping, PathVariable, RestController}
import com.luo.JdbcRepo
import com.luo.test.domain.Student


@SpringBootApplication
class StudentController {


  @RestController
  class Controller {


    //计算总分前20名
    @GetMapping(Array("/student/findRankingTwenty"))
    def findRankingTwenty: String= {
      val studentsList = JdbcRepo.queryAllStudents()

      val d= studentsList.sortWith((a:Student,b:Student) => a.chiness+a.math+a.english>b.chiness+b.math+b.english)

      for (a <- 0 to 19){
        println("第"+(a+1)+"名："+d(a).name+" 分数："+sum(d,a))
      }
      return "success"
    }

    //计算各科男女状元
    //  sex传男女
    @GetMapping(Array("/student/findFirstSubject"))
    def findFirstSubject(@PathVariable("sex") sex: Int): String= {
      val studentsList = JdbcRepo.queryAllStudents()
      val l = studentsList.filter(_.sex==sex)
      val chiness = l.sortWith((a:Student,b:Student) => a.chiness>b.chiness).head
      val math = l.sortWith((a:Student,b:Student) => a.math>b.math).head
      val english = l.sortWith((a:Student,b:Student) => a.english>b.english).head
      var s=""
      if(sex==0){
        s="女"
      }else{
        s="男"
      }
      println("语文"+s+"状元分数是"+chiness)
      println("英语"+s+"状元分数是"+english)
      println("数学"+s+"状元分数是"+math)
      return "success"
    }


     //计算每班科目前五名
     @GetMapping(Array("/student/findFive"))
     def findFive (@PathVariable("classes") classes: String): String= {
       val studentsList = JdbcRepo.queryAllStudents()
       val l = studentsList.filter(_.classes==classes)
       val l1= l.sortWith((a:Student,b:Student) => a.math>b.math)
       val l2= l.sortWith((a:Student,b:Student) => a.chiness>b.chiness)
       val l3= l.sortWith((a:Student,b:Student) => a.english>b.english)
       for (a <- 0 to 4){

         println(classes+"班的数学成绩排名第"+(a+1)+"名    姓名："+l1(a).name+"  成绩："+l1(a).math)
         println(classes+"班的语文成绩排名第"+(a+1)+"名    姓名："+l2(a).name+"  成绩："+l2(a).chiness)
         println(classes+"班的英语成绩排名第"+(a+1)+"名    姓名："+l3(a).name+"  成绩："+l3(a).english)
       }
       return "success"
     }

    //每个班各科平均分
    @GetMapping(Array("/student/findAverage"))
    def findAverage (@PathVariable("classes") classes: String): String= {
      val studentsList = JdbcRepo.queryAllStudents()
      val l = studentsList.filter(_.classes==classes)
      val l1= l.sortWith((a:Student,b:Student) => a.math>b.math)
      val l2= l.sortWith((a:Student,b:Student) => a.chiness>b.chiness)
      val l3= l.sortWith((a:Student,b:Student) => a.english>b.english)
      val averageMath=l1.map(_.math).sum/l1.length
      val averageEnglish=l3.map(_.english).sum/l3.length
      val averageChiness=l2.map(_.chiness).sum/l2.length
      println(classes+"数学平均成绩是:"+averageMath);
      println(classes+"语文平均成绩是:"+averageChiness);
      println(classes+"英语平均成绩是:"+averageEnglish);

      return "success"
    }


    //每个班各科平均分
    @GetMapping(Array("/student/findProportion"))
    def findProportion (@PathVariable("classes") classes: String): String= {
      val studentsList = JdbcRepo.queryAllStudents()
      val l = studentsList.filter(_.classes==classes)
      val l1= l.sortWith((a:Student,b:Student) => a.math>b.math).map(_.math)
      val l2= l.sortWith((a:Student,b:Student) => a.chiness>b.chiness).map(_.chiness)
      val l3= l.sortWith((a:Student,b:Student) => a.english>b.english).map(_.english)
      val you=l1.filter(_>=90).length/l.length
      val liang=l1.filter(_ >=75).filter(_<90).sum/l.length
      val jige=l1.filter(_ >=60).filter(_<75).sum/l.length
      val bujige=l1.filter(_ <60).sum/l.length
      println(classes+"优秀是:"+you);
      println(classes+"良是:"+liang);
      println(classes+"及格是:"+jige);
      println(classes+"不及格是:"+bujige);

      return "success"
    }




    }

  def sum(s:List[Student],index:Int):Double={
    s(index).chiness+s(index).math+s(index).english
  }




}









object Start extends App {
  SpringApplication.run(classOf[StudentController])
}